//
//  Tools.swift
//  eBooks
//
//  Created by JunMing on 2019/11/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

import UIKit

class Tools: NSObject {
    static let host = "http://47.105.185.248/Source/"
    static func hostName(_ name:String) ->String {
        return host + absUrl(bookUrl: name)!
    }
    
    static func epubUrl() ->String {
        return host + "epubs/little%20prince.epub"
    }
    
    static func epubInfoSqlite() ->String? {
        if let docuPath = documentPath() {
            return docuPath+"/epubInfo.sqlite"
        }else {
            return nil
        }
    }
    
    static func documentPath() ->String? {
        let documentDir = FileManager.SearchPathDirectory.documentDirectory
        let domainMask = FileManager.SearchPathDomainMask.allDomainsMask
        return NSSearchPathForDirectoriesInDomains(documentDir,domainMask, true).first
    }
    
    static func isBookExist(bookName name:String) ->Bool {
        if let docuPath = documentPath() {
            let toPath = docuPath+"/"+name
            return FileManager.default.fileExists(atPath: toPath)
        }else {
            return false
        }
    }
    
    static func absUrl(bookUrl url:String) ->String? {
        return url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    }
    
    static func unarchiver(callback: @escaping (_ dic:Dictionary<String,String>)->Void) {
        if let path = documentPath() {
            DispatchQueue.global().async {
                let data = NSKeyedUnarchiver.unarchiveObject(withFile: path+"/ebook.archiver")
                DispatchQueue.main.async {
                    callback(data as! Dictionary<String, String>)
                }
            }
        }
    }
    
    static func archiver(object:Dictionary<String, String>) {
        if let path = documentPath() {
            DispatchQueue.global().async {
                NSKeyedArchiver.archiveRootObject(object, toFile: path+"/ebook.archiver")
            }
        }
    }
    
    static func getVip()->Bool {
        return EBookDefault.readBoolByKey("user_vip")
    }
    
    static func setVip(_ value:Bool) {
        EBookDefault.setBool(value, "user_vip")
    }
    
    static func getIP()->String? {
        return EBookDefault.readStringByKey("wifi_ip")
    }
    
    static func setIP(_ value:String) {
        EBookDefault.setString(value, "wifi_ip")
    }
    
    static func showGuide(_ sucess:Bool) -> Bool {
        if sucess {
            EBookDefault.setInteger(0, "guide")
        }else {
            let count = EBookDefault.readIntegerByKey("guide")+1
            EBookDefault.setInteger(count, "guide")
        }
        
        if EBookDefault.readIntegerByKey("guide") > 3 {
            EBookDefault.setInteger(0, "guide")
            return true
        }
        return false
    }
    
    static func umengKey() ->String {
        return "5e5f745a0cafb24dd5000117"
    }
    
    /// 是否垂直的
    static func getVertical() -> Bool {
        return EBookDefault.readBoolByKey("Vertical")
    }
    
    static func switchVertical(){
        let status = !getVertical()
        EBookDefault.setBool(status, "Vertical")
    }
    
    /// 是否垂直的
    static func showYinSi() -> Bool {
        return !EBookDefault.readBoolByKey("yinsi")
    }
}
